package com.mofang.activity.user;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.mofang.R;
import com.mofang.activity.BaseTitleViewActiviey;
import com.mofang.adapters.AreaSelectAdapter;
import com.mofang.db.ExternalDBManager;
import com.pactera.framework.util.Loger;

public class AreaSelectActivity extends BaseTitleViewActiviey {

	private static final int slelct_type_pro=0;
	private static final int slelct_type_city=1;
	private static final int slelct_type_area=2;
	private static int currentSelectType=slelct_type_pro;
	
	private ListView lv_area_p;
	private ListView lv_area_c;
	private ListView lv_area_a;
	AreaSelectAdapter  pro_adapter;
	AreaSelectAdapter city_adapter;
	AreaSelectAdapter area_adapter;
	private List<Area> proset = new ArrayList<Area>();// 省份集合
	private List<Area> cityset = new ArrayList<Area>();// 城市份集合
	private List<Area> areaset = new ArrayList<Area>();// 地区集合


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_area_select);
		super.onCreate(savedInstanceState);
		setNavigationTitle("选择省份");
		setNavigationLeftBtnImage(R.drawable.return_arrow);
	}

	@Override
	protected void initView() {
		super.initView();
		lv_area_p = (ListView) findViewById(R.id.lv_area_p);
		lv_area_c = (ListView) findViewById(R.id.lv_area_c);
		lv_area_a = (ListView) findViewById(R.id.lv_area_a);
		lv_area_c.setVisibility(View.GONE);
		lv_area_a.setVisibility(View.GONE);
	}

	@Override
	protected void setListener() {
		super.setListener();
		proset = getProSet(0);
		pro_adapter = new AreaSelectAdapter(this,proset);
		city_adapter = new AreaSelectAdapter(this,cityset);
		area_adapter = new AreaSelectAdapter(this,areaset);

		lv_area_p.setAdapter(pro_adapter);
		lv_area_p.setOnItemClickListener(new SelectProvince());
		lv_area_c.setAdapter(city_adapter);
		lv_area_c.setOnItemClickListener(new SelectCity());
		lv_area_a.setAdapter(area_adapter);
		lv_area_a.setOnItemClickListener(new SelectArea());

	}

	// 选择改变状态
	class SelectProvince implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// 获得省份ID
			// ToastHelper.getInstance().showLongMsg(proNameset.get(position));
			cityset.clear();
			cityset =getProSet(proset.get(position).id);
			city_adapter.setData(cityset);
			currentSelectType=slelct_type_city;
			setNavigationTitle("选择城市");
			lv_area_p.setVisibility(View.GONE);
			lv_area_c.setVisibility(View.VISIBLE);
			city_adapter.notifyDataSetChanged();
		}
	}

	// 城市 选择改变状态
	class SelectCity implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			areaset.clear();
			areaset =getProSet(cityset.get(position).id);
			area_adapter.setData(areaset);
			currentSelectType=slelct_type_area;
			setNavigationTitle("选择地区");
			lv_area_c.setVisibility(View.GONE);
			lv_area_a.setVisibility(View.VISIBLE);
			area_adapter.notifyDataSetChanged();
			
		}
	}

	// 城市 选择改变状态
	class SelectArea implements OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			//ToastHelper.getInstance().showLongMsg(areaNameset.get(position));
			Intent intent=new Intent();
			intent.putExtra("id", areaset.get(position).id);
			intent.putExtra("name", areaset.get(position).name);
			setResult(AreaSelectActivity.this.RESULT_OK,intent);
			AreaSelectActivity.this.finish();
		}
	}
	@Override  
    public boolean onKeyDown(int keyCode, KeyEvent event)  
    {
        if (keyCode == KeyEvent.KEYCODE_BACK )  
        {
        	switch (currentSelectType) {
        	case slelct_type_pro:
        		Loger.d("proc 返回");
        		setResult(RESULT_CANCELED);
        		finish();
        		break;
        	case slelct_type_city:
        		Loger.d("city 返回");
        		setNavigationTitle("选择省份");
        		lv_area_p.setVisibility(View.VISIBLE);
    			lv_area_c.setVisibility(View.GONE);
    			currentSelectType=slelct_type_pro;
        		break;
			case slelct_type_area:
				Loger.d("area 返回");
				setNavigationTitle("选择城市");
				lv_area_c.setVisibility(View.VISIBLE);
    			lv_area_a.setVisibility(View.GONE);
    			currentSelectType=slelct_type_city;
				break;
			default:
				break;
			}
        }  
        return false;  
    }  

	/**
	 * 返回 省份集合
	 */
	public List<Area> getProSet(int pid) {
		// 打开数据库
		List<Area> list = new ArrayList<AreaSelectActivity.Area>();
		SQLiteDatabase db1 = new ExternalDBManager(mContext).openDatabase();
		String selection = "Parent_id =?";
		String[] selectionArgs = { String.valueOf(pid) };
		String orderBy = "Id ASC";
		Cursor cursor = db1.query("areas", null, selection, selectionArgs,
				null, null, orderBy);
		while (cursor.moveToNext()) {
			int id = cursor.getInt(cursor.getColumnIndexOrThrow("AreasID"));
			String name = cursor
					.getString(cursor.getColumnIndexOrThrow("Name"));
			Area area = new Area(id, name);
			// proNameset.add(name);
			list.add(area);
		}
		cursor.close();
		db1.close();
		return list;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.area_select, menu);
		return true;
	}

	public class Area {
		public int id;
		public String name;

		public Area(int id, String name) {
			super();
			this.id = id;
			this.name = name;
		}

	}
}
